package com.lp.controller;

import com.lp.aspect.HttpAspect;
import com.lp.domain.Girl;
import com.lp.domain.Result;
import com.lp.repository.GirlRepository;
import com.lp.service.GirlService;
import com.lp.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by lp
 * 2017/11/7 9:48
 */
@RestController
public class GirlController {
    private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 添加女生
     * @return
     */
//    @PostMapping("/girls")
//    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
//                        @RequestParam("age") Integer age){
//        Girl girl = new Girl();
//        girl.setCupSize(cupSize);
//        girl.setAge(age);
//        return  girlRepository.save(girl);
//    }

    @PostMapping(value="/girls")
    public Result<Girl> girlAdd(@Valid Girl girl,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
//            return new Result(1,bindingResult.getFieldError().getDefaultMessage(),null);
            return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        //girl.setCupSize(girl.getCupSize());
        //girl.setAge(girl.getAge());
//        return  new Result(0,"",girlRepository.save(girl));
        return  ResultUtil.success(girlRepository.save(girl));
    }
    /**
     * 删除女生
     * @return
     */
    @DeleteMapping(value="/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    /**
     * 更新女生
     * @return
     */
    @PutMapping(value="/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return  girlRepository.save(girl);
    }

    /**
     * 查询女生列表
     * @return
     */
    //@RequestMapping(value="/findAll", method=RequestMethod.GET)
    @GetMapping("/girls")
    public List<Girl> girlList(){
        logger.info("girlList ......");
        return  girlRepository.findAll();
    }

    /**
     * 查询女生
     * @return
     */
    @GetMapping(value="/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return  girlRepository.findOne(id);
    }

    /**
     * 通过年龄查询女生
     * @return
     */
    @GetMapping(value="/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 通过年龄查询女生
     * @return
     */
    @GetMapping(value="/girls/age")
    public List<Girl> girlListByAge2(@RequestParam("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     *
     * @return
     */
    @PostMapping(value="/girls/two")
    public void insertTwo(){
        girlService.insertTwo();
    }

    /**
     *
     * @return
     */
    @GetMapping(value="/girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception{
        girlService.getAge(id);
    }
}
